import java.util.ArrayList;
import java.awt.Point;

import javax.swing.JOptionPane;

public class AvailableBlocks {
	private static final int BLACK = 0;
	private static final int WHITE = 1;
	private int target;
	private boolean flag;	// to see the continuous of disks
	 
	private ArrayList<Point> available;
	private Board board;
	
	public AvailableBlocks(){
		available = new ArrayList<Point>();
		board = new Board();
		flag = false;
	}
	public void calc(Board b, int t){
		board = b;
		target = t;
		available.clear();
		if(target==BLACK)
			for(int i=0;i<b.getBlack().size();i++)
				search8(b.getBlack().get(i));
		else if(target==WHITE)
			for(int i=0;i<b.getWhite().size();i++)
				search8(b.getWhite().get(i));

	}
	public ArrayList<Point> getAvailableBlocks(){
		return available;
	}
	private void search8(Point p){
		search(p,new Point(p.x,p.y-1));
		search(p,new Point(p.x-1,p.y-1));
		search(p,new Point(p.x-1,p.y));
		search(p,new Point(p.x-1,p.y+1));
		search(p,new Point(p.x,p.y+1));
		search(p,new Point(p.x+1,p.y+1));
		search(p,new Point(p.x+1,p.y));
		search(p,new Point(p.x+1,p.y-1));
	}
	private void search(Point original, Point p){
		if(board.checkBlock(p)==-1&&flag){
			if(!available.contains(p)&&p.x>=1&&p.x<=8&&p.y>=1&&p.y<=8)
				available.add(p);
			flag = false;
		}
		else if(board.checkBlock(p)==target){
			flag = false;
		}
		else if(board.checkBlock(p)!=-1&&board.checkBlock(p)!=target){
			flag = true;
				search(p,new Point(p.x+(p.x-original.x),p.y+(p.y-original.y)));
		}
	}
}
